package com.example.phoneshop.controller.admin;

import com.example.phoneshop.config.Config;
import com.example.phoneshop.entity.User;
import com.example.phoneshop.tool.Check;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@SuppressWarnings("Duplicates")
@Controller
@RequestMapping(value = "admin")
public class AdminController {
    private String[] menu;

    @RequestMapping(value = {"index", "/", ""}, method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
        // 判断是否登录
        if (AdminOrderController.isAdminLogin(session, response)){
            return null;
        }
        ModelAndView model = new ModelAndView("admin/index/index");
        // 获取导航栏的active数组
        menu = Config.getMenu();
        model.addObject("user", (User) session.getAttribute("user"));
        model.addObject("title", "后台首页");
        model.addObject("classurl", request.getContextPath() + "/admin/index");
        model.addObject("classname", "首页");
        menu[0] = "active";
        model.addObject("menu", menu);
        return model;
    }

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public ModelAndView login(HttpServletResponse response, HttpSession session, HttpServletRequest request) {
        // 判断是否登录
        if (Check.checkAdminLogin(session)) {
            try {
                response.sendRedirect("/admin/index");
                return null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        ModelAndView model = new ModelAndView("admin/login/index");
        model.addObject("title", "登录");
        return model;
    }
}
